//
//  DiscoverViewController.swift
//  新浪微博
//
//  Created by MBP on 16/5/11.
//  Copyright © 2016年 MBP. All rights reserved.
//

import UIKit

private let DiscoverCellID = "DiscoverViewControllerCell"
class DiscoverViewController: RootViewController {

    override func viewDidLoad() {
        //添加刷新功能
        setupRefreshControl()
        //父类方法会 调用loadData()
        super.viewDidLoad()
        
        //注册cell
        tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: DiscoverCellID)
        
        // 设置访客视图
        visitorView?.setupInfo("visitordiscover_image_message",
            message: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
    }
    //重写loadData() 方法
    override func loadData() {
        //开始刷新
        refreshControl?.beginRefreshing()
        
        let when = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 1.0))
        dispatch_after(when, dispatch_get_main_queue()) { () -> Void in
            //数据扩充
            self.dataCount += 10
            
            //结束刷新
            self.refreshControl?.endRefreshing()
            
            //刷新cell
            self.tableView?.reloadData()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    // MARK: - 私有属性
    private var dataCount = 0
}

extension DiscoverViewController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataCount
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(DiscoverCellID, forIndexPath: indexPath)
        
        cell.textLabel?.text = "\(indexPath.row)"
        
        return cell
    }
}
